package testcase;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class GuiConverter extends JFrame implements ActionListener{
	
	
	JFileChooser fc;
	public GuiConverter () {
		
		
		super("Convert xls to xml");
		setSize(570,180);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		FlowLayout fl = new FlowLayout();
		setLayout(fl);
		
		JLabel label = new JLabel("This Programm will convert your xls Files (Excel) to xml Files.");
		setVisible(true);
		label.setFont(new Font("sansserif", Font.BOLD, 18));
		label.setAlignmentX(CENTER_ALIGNMENT);
		add(label); 
		
		JButton converterbutton = new JButton("Convert!");
		converterbutton.addActionListener(this);
		add(converterbutton);
		setVisible(true);
		converterbutton.setAlignmentX(CENTER_ALIGNMENT);
		converterbutton.setFont(new Font("sansserif", Font.BOLD, 28));
		
		
		
		JLabel label2 = new JLabel("xml File will be saved at choosen Directory as *filename*.xml");
		setVisible(true);
		label2.setFont(new Font("sansserif", Font.BOLD, 18));
		add(label2);
		
		fc = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel 2003 File", "xls");
		fc.setFileFilter(filter);
		
		setVisible(true);
		fc.showOpenDialog(fc);
		fc.getSelectedFile();
		System.out.println(fc.getSelectedFile().toString());
		
	}

	

	public static void main (String[] args ){
		GuiConverter frame = new GuiConverter(); 
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		TestCaseConverter converter = new TestCaseConverter(fc.getSelectedFile());
		try {
			converter.read();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}
